
#include "stdafx.h"
#include "ParamRebid.h"

void CPropRebid::setDefault(intptr_t id)
{
	rebidTime.setDefault();
	rebidTime.bidSecond += id; rebidTime.bidtm.second += (int)id;
	for (intptr_t i = 0; i < PROPREBID_NUM; i++)
	{
		procRebid[i] = false;
		rebid[i].setDefault();
	}
	prcDiffMin = 0;
	prcDiffMax = 19;
}

void CPropRebid::readProp(const CINIOperater *oper, LPCWSTR section, CPropRebid&def)
{
	rebidTime.readProp(oper, section, def.rebidTime);
	prcDiffMin = oper->GetIntWithRange(section, L"PriceDiffMin", (int)def.prcDiffMin, 0, 19);
	prcDiffMax = oper->GetIntWithRange(section, L"PriceDiffMax", (int)def.prcDiffMax, 0, 19);
	for (intptr_t i = 0; i < PROPREBID_NUM; i++)
	{
		wchar_t sec[100]; swprintf_s(sec, L"%s\\Condition%d", section, (int)i);
		procRebid[i] = oper->GetBoolean(sec, L"ProcRebid", def.procRebid[i]);
		rebid[i].readProp(oper, sec, def.rebid[i]);
	}
}

void CPropRebid::writeProp(CINIOperater *oper, LPCWSTR section) const
{
	rebidTime.writeProp(oper, section);
	oper->WriteInt(section, L"PriceDiffMin", (int)prcDiffMin);
	oper->WriteInt(section, L"PriceDiffMax", (int)prcDiffMax);
	for (intptr_t i = 0; i < PROPREBID_NUM; i++)
	{
		wchar_t sec[100]; swprintf_s(sec, L"%s\\Condition%d", section, (int)i);
		oper->WriteBoolean(sec, L"ProcRebid", procRebid[i]);
		rebid[i].writeProp(oper, sec);
	}
}

bool CPropRebid::isEqualProp(const CPropRebid &p) const
{
	if (!rebidTime.isEqualProp(p.rebidTime) ||
		prcDiffMin != p.prcDiffMin ||
		prcDiffMax != p.prcDiffMax) return false;
	for (intptr_t i = 0; i < PROPREBID_NUM; i++)
	{
		if (procRebid[i] != p.procRebid[i] ||
			!rebid[i].isEqualProp(p.rebid[i])) return false;
	}
	return true;
}


#ifndef _SKEYMINI
static CParamRebid defaultRebid;
CParamRebid& CParamRebid::defaultValue()
{
	if (!defaultRebid.m_initialized)
	{
		for (intptr_t i = 0; i < PROPREBID_PLANNUM; i++) defaultRebid.plan[i].setDefault(i);
		defaultRebid.m_initialized = true;
	}
	return defaultRebid;
}
#else
static CParamRebid defaultRebid[] = {
	CParamRebid(false), CParamRebid(true)
};
CParamRebid& CParamRebid::defaultValue(bool bSimulate)
{
	intptr_t id = (bSimulate ? 1 : 0);
	if (!defaultRebid[id].m_initialized)
	{
		if (!bSimulate)
			defaultRebid[id].ReadSimulate();
		else
			for (intptr_t i = 0; i < PROPREBID_PLANNUM; i++) defaultRebid[id].plan[i].setDefault(i);
		defaultRebid[id].m_initialized = true;
	}
	return defaultRebid[id];
}
#endif

void CParamRebid::readINI(const CINIOperater *oper)
{
	CParamRebid &def = CParamRebid::defaultValue(
#ifdef _SKEYMINI
		m_bSimulate
#endif
		);
	for (intptr_t i = 0; i < PROPREBID_PLANNUM; i++)
	{
		wchar_t sec[100]; swprintf_s(sec, L"%s\\Plan%d", section, (int)i);
		plan[i].readProp(oper, sec, def.plan[i]);
	}
}

void CParamRebid::writeINI(CINIOperater *oper) const
{
	for (intptr_t i = 0; i < PROPREBID_PLANNUM; i++)
	{
		wchar_t sec[100]; swprintf_s(sec, L"%s\\Plan%d", section, (int)i);
		plan[i].writeProp(oper, sec);
	}
}

bool CParamRebid::isEqual(const CParamRebid &p) const
{
	for (intptr_t i = 0; i < PROPREBID_PLANNUM; i++) if (!plan[i].isEqualProp(p.plan[i])) return false;
	return true;
}
